
use "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\CPSBasic.dta", clear

* merge in David Dorn's Occupation Codes *
rename occ1990 occ90
merge m:1 occ90 using "${path}\Clean Data\Crosswalks\occ1990_occ1990dd.dta"
keep if _merge == 3
drop _merge

merge m:1 occ1990dd using "${path}\Clean Data\Crosswalks\occ1990dd_task_alm.dta", nogenerate
drop occ
rename occ1990dd occ

keep if year < 2008 /* Keep only years for which I have Romer and Romer shock var */
keep if age > 24 & age < 66 /* Drop non-working-age */

drop if occ >= 473 & occ <= 498 /* Drop farm workers */

gen employed = 0
gen unemployed = 0
gen total = 1

replace employed = 1 if empstat < 15
replace unemployed = 1 if empstat > 15

* Merge occupational categories from main analysis * 

merge m:1 occ using "${path}\Clean Data\OccCategories.dta"

gen Month = (year-1979)*12 + month

sort cpsidv cpsid pernum sex race Month

gen lastjob = occ[_n-1] if cpsidv== cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month== Month[_n-1]

gen nextjob = occ[_n+1] if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 3 & OCC == 2 


***************************************
* J2J Transitions: Routine and Abstract
***************************************

gen Rou2Abs = 0
replace Rou2Abs = 1 if cpsidv== cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month== Month[_n-1] + 1 & OCC== 3 & OCC[_n-1] == 2 

gen Abs2Rou = 0
replace Abs2Rou = 1 if cpsidv == cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month == Month[_n-1] + 1 & OCC == 2 & OCC[_n-1] == 3 


***************************************
* J2J Transitions: Routine and Manual
***************************************

gen Rou2Man = 0
replace Rou2Man = 1 if cpsidv == cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month == Month[_n-1] + 1 & OCC == 1 & OCC[_n-1] == 2 

gen Man2Rou = 0
replace Man2Rou = 1 if cpsidv == cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month == Month[_n-1] + 1 & OCC == 2 & OCC[_n-1] == 1 


***************************************
* J2J Transitions: Manual and Abstract
***************************************

gen Man2Abs = 0
replace Man2Abs = 1 if cpsidv == cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month == Month[_n-1] + 1 & OCC == 3 & OCC[_n-1] == 1 

gen Abs2Man = 0
replace Abs2Man = 1 if cpsidv == cpsidv[_n-1] & sex == sex[_n-1] & race == race[_n-1] & Month == Month[_n-1] + 1 & OCC == 1 & OCC[_n-1] == 3 

***************************************
* J2J Transitions: Routine and Abstract
***************************************

gen Rou2Abs2 = 0
replace Rou2Abs2 = 1 if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 3 & OCC == 2 

gen Abs2Rou2 = 0
replace Abs2Rou2 = 1 if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 2 & OCC == 3 


***************************************
* J2J Transitions: Routine and Manual
***************************************

gen Rou2Man2 = 0
replace Rou2Man2 = 1 if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 1 & OCC == 2 


gen Man2Rou2 = 0
replace Man2Rou2 = 1 if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 2 & OCC == 1 


***************************************
* J2J Transitions: Manual and Abstract
***************************************

gen Man2Abs2 = 0
replace Man2Abs2 = 1 if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 3 & OCC == 1 

gen Abs2Man2 = 0
replace Abs2Man2 = 1 if cpsidv[_n+1] == cpsidv & Month[_n+1] == Month + 1 & OCC[_n+1] == 1 & OCC == 3 

*******************************************
* J2J Transitions: Routine and Unemployment
*******************************************

gen Rou2UnEmp = 0
replace Rou2UnEmp = 1 if cpsidv == cpsidv[_n+1] & Month == Month[_n+1] - 1 & unemployed[_n+1] == 1 & OCC == 2

gen UnEmp2Rou = 0
replace UnEmp2Rou = 1 if cpsidv == cpsidv[_n+1] & Month == Month[_n+1] - 1 & unemployed == 1 & OCC[_n+1] == 2


******************************************
* J2J Transitions: Unemployment and Manual
******************************************

gen Man2UnEmp = 0
replace Man2UnEmp = 1 if cpsidv == cpsidv[_n+1] & Month == Month[_n+1] - 1 & unemployed[_n+1] == 1 & OCC == 1

gen UnEmp2Man = 0
replace UnEmp2Man = 1 if cpsidv == cpsidv[_n+1] & Month == Month[_n+1] - 1 & unemployed == 1 & OCC[_n+1] == 1


********************************************
* J2J Transitions: Unemployment and Abstract
********************************************

gen Abs2UnEmp = 0
replace Abs2UnEmp = 1 if cpsidv == cpsidv[_n+1] & Month == Month[_n+1] - 1 & unemployed[_n+1] == 1 & OCC == 3

gen UnEmp2Abs = 0
replace UnEmp2Abs = 1 if cpsidv == cpsidv[_n+1] & Month == Month[_n+1] - 1 & unemployed == 1 & OCC[_n+1] == 3

**

gen manual = 0
replace manual = 1 if OCC == 1

gen abstract = 0
replace abstract = 1 if OCC == 3

gen routine = 0
replace routine = 1 if OCC == 2

gen Date = Month

save "${path}\Clean Data\J2J_clean_basic.dta", replace

* Find the most frequent transitioning occupations *

collapse (sum) Rou2Abs Abs2Rou Rou2Man Man2Rou Man2Abs Abs2Man Rou2Abs2 Abs2Rou2 Rou2Man2 Man2Rou2 Man2Abs2 Abs2Man2 abstract routine manual employed unemployed [w=wtfinl], by(occ OCC occ90)

* sum up total transitions *

gen AbsGrowth = (Rou2Abs + Man2Abs) if OCC == 3
gen ManGrowth = (Rou2Man + Abs2Man) if OCC == 1
gen RouGrowth = (Abs2Rou + Man2Rou) if OCC == 2

gen AbsDecline = (Abs2Rou2 + Abs2Man2) if OCC == 3
gen ManDecline = (Man2Rou2 + Man2Abs2) if OCC == 1
gen RouDecline = (Rou2Abs2 + Rou2Man2) if OCC == 2


pctile pctA = AbsGrowth, nq(5)
pctile pctR = RouGrowth, nq(5)
pctile pctM = ManGrowth, nq(5)

pctile pctA_d = AbsDecline, nq(5)
pctile pctR_d = RouDecline, nq(5)
pctile pctM_d = ManDecline, nq(5)

gen pctA80 = pctA[4]
gen pctR80 = pctR[4]
gen pctM80 = pctM[4]

gen pctA80_d = pctA_d[4]
gen pctR80_d = pctR_d[4]
gen pctM80_d = pctM_d[4]

replace AbsGrowth = 0 if AbsGrowth == .
replace AbsDecline = 0 if AbsDecline == .
replace RouGrowth = 0 if RouGrowth == .
replace RouDecline = 0 if RouDecline == .
replace ManGrowth = 0 if ManGrowth == .
replace ManDecline = 0 if ManDecline == .

save "${path}\Clean Data\J2J1.dta", replace

* Find share of total transitions attributed to top quintile *

collapse (sum) AbsGrowth AbsDecline RouGrowth RouDecline ManGrowth ManDecline, by(OCC)

rename AbsGrowth AbsGrowth_t
rename AbsDecline AbsDecline_t
rename RouGrowth RouGrowth_t
rename RouDecline RouDecline_t
rename ManGrowth ManGrowth_t
rename ManDecline ManDecline_t

save "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\J2J2.dta", replace

* Identify top transitioning occupations *

use "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\J2J1.dta", clear


keep if AbsGrowth >= pctA80 | AbsDecline >= pctA80_d | RouGrowth >= pctR80 | RouDecline >= pctA80_d | ManGrowth >= pctM80 | ManDecline >= pctM80_d

keep occ90

save "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\TopTransitioners.dta", replace

use "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\J2J1.dta", clear


keep if AbsGrowth >= pctA80 | AbsDecline >= pctA80_d | RouGrowth >= pctR80 | RouDecline >= pctA80_d | ManGrowth >= pctM80 | ManDecline >= pctM80_d

collapse (sum) AbsGrowth AbsDecline RouGrowth RouDecline ManGrowth ManDecline, by(OCC)
merge 1:1 OCC using "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\J2J2.dta"

drop _merge

gen AShare = AbsGrowth/AbsGrowth_t
gen RShare = RouGrowth/RouGrowth_t
gen MShare = ManGrowth/ManGrowth_t

gen AShare2 = AbsDecline/AbsDecline_t
gen RShare2 = RouDecline/RouDecline_t
gen MShare2 = ManDecline/ManDecline_t

* Find average monthly net growth rates from (1) other task groups (2) non-employment

use "${path}\Clean Data\J2J_clean_basic.dta", clear

collapse (sum) Rou2Abs2 Abs2Rou2 Rou2Man2 Man2Rou2 Man2Abs2 Abs2Man2 Rou2UnEmp Abs2UnEmp Man2UnEmp UnEmp2Abs UnEmp2Man UnEmp2Rou abstract manual routine [w=wtfinl], by(Month)

* generate year-over-year monthly growth rates *

gen AbsGrowth = (Rou2Abs2 + Man2Abs2)/abstract 
gen ManGrowth = (Rou2Man2 + Abs2Man2)/manual
gen RouGrowth = (Abs2Rou2 + Man2Rou2)/routine

gen AbsDecline = (Abs2Rou2 + Abs2Man2)/abstract 
gen ManDecline = (Man2Abs2 + Man2Rou2)/manual
gen RouDecline = (Rou2Abs2 + Rou2Man2)/routine

gen AbsGrowthUn = UnEmp2Abs/abstract
gen ManGrowthUn = UnEmp2Man/manual
gen RouGrowthUn = UnEmp2Rou/routine

gen AbsDeclineUn = Abs2UnEmp/abstract
gen ManDeclineUn = Man2UnEmp/manual
gen RouDeclineUn = Rou2UnEmp/routine

gen N=1

collapse (mean) AbsGrowth AbsGrowthUn AbsDecline AbsDeclineUn RouGrowth RouGrowthUn RouDecline RouDeclineUn ManGrowth ManGrowthUn ManDecline ManDeclineUn, by(N)

* Find total share of flexible occupations *

use "${path}\Clean Data\J2J_clean_basic.dta", clear

gen N = 1
collapse (sum) N [w=wtfinl], by(occ90)

merge 1:1 occ90 using "C:\Users\IRWAM02\Dropbox\Replication\Clean Data\TopTransitioners.dta"

gen Flex = 0
replace Flex = 1 if _merge == 3

gen n = 1

bysort Flex: egen total_flex=total(N)
bysort n: egen total=total(N)

gen flexshare = total_flex/total


